package com.yyl.jni.player;

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.util.Log;
import android.view.Surface;

import com.yyl.jni.opengl.VideoRender;
import com.yyl.jni.opengl.VideoSurfaceView;
import com.yyl.jni.utils.VideoSupportUtils;

import java.nio.ByteBuffer;

public class VideoYUVCodec {

    private YPlayer yPlayer;
    private MediaFormat mediaFormat;
    private MediaCodec mediaCodec;


    private VideoSurfaceView surfaceView;

    private Surface surface;
    private MediaCodec.BufferInfo bufferInfo;

    public VideoYUVCodec(YPlayer yPlayer) {
        this.yPlayer = yPlayer;
    }

    public void setSurfaceView(VideoSurfaceView surfaceView) {
        this.surfaceView = surfaceView;
        surfaceView.getVideoRender().setOnSurfaceCreateListener(new VideoRender.OnSurfaceCreateListener() {
            @Override
            public void onSurfaceCreate(Surface s) {
                surface = s;
                d("onSurfaceCreate");
            }
        });
    }

    //mediacodec
    public void releaseMediacodec() {
        if (mediaCodec != null) {
            try {
                mediaCodec.flush();
                mediaCodec.stop();
                mediaCodec.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
            mediaCodec = null;
            mediaFormat = null;
            bufferInfo = null;
        }
    }


    public void onJNIdecodeAVPacket(int datasize, byte[] data) {
        if (surface != null && datasize > 0 && data != null && mediaCodec != null) {
            try {
                int intputBufferIndex = mediaCodec.dequeueInputBuffer(10);
                if (intputBufferIndex >= 0) {
                    ByteBuffer byteBuffer = mediaCodec.getInputBuffers()[intputBufferIndex];
                    byteBuffer.clear();
                    byteBuffer.put(data);
                    mediaCodec.queueInputBuffer(intputBufferIndex, 0, datasize, 0, 0);
                }
                int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 10);
                while (outputBufferIndex >= 0) {
                    mediaCodec.releaseOutputBuffer(outputBufferIndex, true);
                    outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 10);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public boolean onJNIInitMediaCodec(String codecName, int width, int height, byte[] csd_0, byte[] csd_1) {
        boolean isSupport = false;
        if (surface != null) {
            try {
                surfaceView.getVideoRender().setRenderType(VideoRender.RENDER_MEDIACODEC);
                String mime = VideoSupportUtils.findVideoCodecName(codecName);
                mediaFormat = MediaFormat.createVideoFormat(mime, width, height);
                mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, width * height);
                mediaFormat.setByteBuffer("csd-0", ByteBuffer.wrap(csd_0));
                mediaFormat.setByteBuffer("csd-1", ByteBuffer.wrap(csd_1));
                d(mediaFormat.toString());
                mediaCodec = MediaCodec.createDecoderByType(mime);
                bufferInfo = new MediaCodec.BufferInfo();
                mediaCodec.configure(mediaFormat, surface, null, 0);
                mediaCodec.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
            isSupport = true;
        } else {
            d("surface is null");
            if (yPlayer.onEventListener != null) {
                yPlayer.onEventListener.onEvent(10, 0, 0);
            }
            isSupport = false;
        }
        d("onJNIInitMediaCodec   isSupport" + isSupport);
        return isSupport;
    }

    public void d(String msg) {
        Log.i("VideoYUVCodec", msg);
    }
}
